package com.ruoyi.develop.goodsOrderMaterials.service.impl;

import java.util.List;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
        import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsOrderIn.domain.GoodsOrderIn;
import com.ruoyi.develop.goodsOrderIn.mapper.GoodsOrderInMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsOrderMaterials.mapper.GoodsOrderMaterialsMapper;
import com.ruoyi.develop.goodsOrderMaterials.domain.GoodsOrderMaterials;
import com.ruoyi.develop.goodsOrderMaterials.service.IGoodsOrderMaterialsService;

/**
 * 库存产品拆解物料配置Service业务层处理
 * @author qh
 * @date 2025-03-13
 */
@Service
public class GoodsOrderMaterialsServiceImpl implements IGoodsOrderMaterialsService {
    @Resource
    private GoodsOrderMaterialsMapper goodsOrderMaterialsMapper;

    @Resource
    private GoodsOrderInMapper goodsOrderInMapper;
    /**
     * 查询库存产品拆解物料配置
     * @param id 库存产品拆解物料配置主键
     * @return 库存产品拆解物料配置
     */
    @Override
    public GoodsOrderMaterials selectById(String id) {
        return goodsOrderMaterialsMapper.selectById(id);
    }

    /**
     * 查询库存产品拆解物料配置列表
     * @param entity 库存产品拆解物料配置
     * @return 库存产品拆解物料配置
     */
    @Override
    public List<GoodsOrderMaterials> selectList(GoodsOrderMaterials entity) {
        return goodsOrderMaterialsMapper.selectList(entity);
    }

    @Override
    public List<GoodsOrderMaterials> selectByGoodsId(String goodsId) {
        return goodsOrderMaterialsMapper.selectByGoodsId(goodsId);
    }

    @Override
    public List<GoodsOrderMaterials> selectByGoodsIdMetailId(String goodsId,String materialId) {
        return goodsOrderMaterialsMapper.selectByGoodsIdMetailId(goodsId,materialId);
    }

    /**
     * 新增库存产品拆解物料配置
     * @param entity 库存产品拆解物料配置
     * @return 结果
     */
    @Override
    public int insert(GoodsOrderMaterials entity) {
        if(StringUtils.isBlank(entity.getId())){
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return goodsOrderMaterialsMapper.insert(entity);
    }

    @Override
    public void insertForOrderMaterial(GoodsOrderMaterials entityChose) {
        GoodsOrderIn goodsOrderIn=goodsOrderInMapper.selectById(entityChose.getGoodsId());
        if(BusinessConstants.TYPE_CHAIJIE_NO.equals(goodsOrderIn.getIschaijie()) || StringUtils.isBlank(goodsOrderIn.getIschaijie())){
            goodsOrderIn.setIschaijie(BusinessConstants.TYPE_CHAIJIE_YES);
            goodsOrderInMapper.update(goodsOrderIn);
        }
        List<GoodsOrderIn> rows = entityChose.getColumns();
        if(StringUtils.isNotBlank(entityChose.getGoodsId())){
            if (!rows.isEmpty()) {
                for (GoodsOrderIn entity : rows) {
                    GoodsOrderMaterials goodsOrderMaterials=new GoodsOrderMaterials();
                    goodsOrderMaterials.setGoodsId(entityChose.getGoodsId());
                    goodsOrderMaterials.setMaterialId(entity.getId());
                    goodsOrderMaterials.setGoodsCode(entity.getCode());
                    goodsOrderMaterials.setGoodsName(entity.getName());
                    goodsOrderMaterials.setGoodsType(entity.getGoodsType());
                    goodsOrderMaterials.setGoodsBrand(entity.getBrand());
                    goodsOrderMaterials.setGoodModel(entity.getModel());
                    goodsOrderMaterials.setGoodsUnit(entity.getUnit());
                    goodsOrderMaterials.setGoodsCount(entity.getMateriaCount());
                    goodsOrderMaterials.setGoodsBuyPrice(entity.getCostPrice());
                    goodsOrderMaterials.setGoodsBuyTotal(entity.getMateriaCount().multiply(entity.getCostPrice()));
                    goodsOrderMaterials.setRemark(entity.getRemark());
                    insert(goodsOrderMaterials);
                }
            }
        }
    }
    /**
     * 修改库存产品拆解物料配置
     * @param entity 库存产品拆解物料配置
     * @return 结果
     */
    @Override
    public int update(GoodsOrderMaterials entity) {
                entity.setUpdateBy(SecurityUtils.getUsername());
                entity.setUpdateTime(DateUtils.getNowDate());
        return goodsOrderMaterialsMapper.update(entity);
    }

    /**
     * 批量删除库存产品拆解物料配置
     * @param ids 需要删除的库存产品拆解物料配置主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return goodsOrderMaterialsMapper.deleteByIds(ids);
    }

    /**
     * 删除库存产品拆解物料配置信息
     * @param id 库存产品拆解物料配置主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        return goodsOrderMaterialsMapper.deleteById(id);
    }
}
